<?php

class Control_Model_Table_Contact extends Zend_Db_Table_Abstract {

    protected $_name = 'contact_content';

    public function getGoogleLink() {
        $select = $this->select();
        $select->order('id DESC')
                ->where('type=?', 'google_link')
                ->limit('1');
        $GoogleLink = $this->_db->query($select)->fetchAll();
        return $GoogleLink[0]['link'];
    }

    public function getAdress() {
        $select = $this->select();
        $select->order('id ASC')
                ->where('type=?', 'adress')
                ->limit('10');
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

    public function getPhone() {
        $select = $this->select();
        $select->order('id ASC')
                ->where('type=?', 'phone')
                ->limit('10');
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

    public function getUrls() {
        $select = $this->select();
        $select->order('id ASC')
                ->where('type=?', 'link')
                ->limit('10');
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

    public function getAdressLimit($limit) {
        $select = $this->select();
        $select->order('id ASC')
                ->where('type=?', 'adress')
                ->limit($limit);
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

    public function getPhoneLimit($limit) {
        $select = $this->select();
        $select->order('id ASC')
                ->where('type=?', 'phone')
                ->limit($limit);
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

    public function getUrlsLimit($limit) {
        $select = $this->select();
        $select->order('id ASC')
                ->where('type=?', 'link')
                ->limit($limit);
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

        public function getAllData() {
        $select = $this->select();
        $select->order('id ASC')
                ->where('id>?','1');
        $Data = $this->_db->query($select)->fetchAll();
        return $Data;
    }

    public function addData($type, $title, $link, $mail) {
        $data = array(
            'type' => $type,
            'title' => $title,
        );
        if (isset($link)&&$link != "") {
            $logo = array('link' => $link);
            $data = $data + $logo;
        }
        if (isset($mail)&&$mail != "") {
            $logo = array('mail' => $mail);
            $data = $data + $logo;
        };
        $this->insert($data);
    }

    public function updateData($id, $type, $title, $link, $mail) {
        $data = array(
            'type' => $type,
            'title' => $title,
        );
        if (isset($link)&&$link != "") {
            $logo = array('link' => $link);
            $data = $data + $logo;
        }
        if (isset($mail)&&$mail != "") {
            $logo = array('mail' => $mail);
            $data = $data + $logo;
        };
        $this->update($data, 'id= ' . (int) $id);
    }

    public function deleteData($id) {
        $this->delete('id =' . (int) $id);
    }

    public function getDataID($id) {
        $id = (int) $id;
        $row = $this->fetchRow('id =' . $id);
        if (!$row) {
            //           throw new Exception("Count not find row $id");
        } else {
            return $row->toArray();
        }
    }

        public function setGoogleLink($link) {
        $data = array(
            'link' => $link
        );
        $this->update($data, 'id= ' . '1');
    }

        public function getGoogle() {
        $select = $this->select();
        $select->order('id DESC')
                ->where('type=?', 'google_link')
                ->where('id=?','1')
                ->limit('1');
        $GoogleLink = $this->_db->query($select)->fetchAll();
        return $GoogleLink;
    }

}

